//
//  LGTRWZPostTopViewCell.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/28.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit

class LGTRWZPostTopViewCell: UITableViewCell {

    //var cornerBackView:LGTCornerView?
    var cornerBackView:UIImageView?
    var stateLabel:UILabel?
    var completeCountLabel:UILabel?
    var unCompleteCountLabel:UILabel?
    var leastTimeLabel:UILabel?
    var dayLabel:UILabel?
    var hourLabel:UILabel?
    var minuteLabel:UILabel?
    var secondLabel:UILabel?
    var offlineDate:Date?
    var timer:Timer?
    var statueScrollView:UIScrollView?
    var countLabelArray:Array<UILabel> = []
    var descLabelArray:Array<UILabel> = []
    var clickBlock:ClickIndexBlock?
    
    private var _model:Dictionary<String, Any>?
    var model:Dictionary<String, Any>{
        get{
            return _model ?? Dictionary.init()
        }set{
            _model = newValue
            self.setData()
        }
    }
    
    func setData(){
        
        let statusKey:String = "\(self.model["status"] as? Int ?? 0)"
        let statusInfo:Dictionary<String, Any> =  TaskDetailInfo[statusKey] as? Dictionary<String, Any> ?? [:]
        let title:String = statusInfo["title"] as? String ?? ""
        self.stateLabel?.text = title
        self.cornerBackView?.image = UIImage.init(named: statusInfo["icon"] as? String ?? "")
        
        self.completeCountLabel?.text = self.model["success_count"] as? String ?? ""
        self.completeCountLabel?.text = self.model["free_count"] as? String ?? ""
        
        //offline
        let offlineStr:String = self.model["offline"] as? String ?? ""
        self.offlineDate = BaseUtil.stringConvertDate(string: offlineStr)
        self.createTimer()
       
        
        //未完成状况
        //如果界面已经布局
        if self.countLabelArray.count > 0{
            return
        }
        
        let x0:CGFloat = SizeFitFloat(12)
        let itemW:CGFloat = SizeFitFloat(67)
        let order_statistics_list:Array<Dictionary<String, Any>> = self.model["order_statistics_list"] as? Array<Dictionary<String, Any>> ?? []
        let count = order_statistics_list.count
        for i in 0..<count{
            let orderDict:Dictionary<String, Any> = order_statistics_list[i] ?? [:]
            let countStr:String = "\(orderDict["value"] as? Int ?? 0)"
            let name:String = "\(orderDict["name"] as? String ?? "")"
            let countLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: SizeFitFloat(12), text: countStr)
            statueScrollView?.addSubview(countLabel)
            countLabel.snp.makeConstraints { (make) in
                make.left.equalTo(x0 + CGFloat(i) * itemW)
                make.height.equalTo(SizeFitFloat(12))
                make.width.equalTo(itemW)
                make.top.equalTo(0)
            }
            self.countLabelArray.append(countLabel)
            
            let descLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: SizeFitFloat(12), text: name)
            statueScrollView?.addSubview(descLabel)
            descLabel.snp.makeConstraints { (make) in
                make.left.equalTo(x0 + CGFloat(i) * itemW)
                make.height.equalTo(SizeFitFloat(12))
                make.width.equalTo(itemW)
                make.top.equalTo(SizeFitFloat(22))
            }
            self.descLabelArray.append(descLabel)
        }
        
        
        self.statueScrollView?.isScrollEnabled = true
        self.statueScrollView?.showsHorizontalScrollIndicator = false
        self.statueScrollView?.contentSize = CGSize.init(width: itemW * CGFloat(count) + x0 * 2, height: 0)
    }
    
    func createTimer(){
        let date:Date = Date.init()
        self.timer?.invalidate()
        if self.offlineDate == nil{
            return
        }
        
        if date.timeIntervalSince1970 < self.offlineDate!.timeIntervalSince1970{
            
            weak var weakSelf = self
            self.timer = Timer.init(timeInterval: 1, repeats: true, block: { (timer:Timer) in
                let newDate = Date.init()
                 if newDate.timeIntervalSince1970 >= weakSelf!.offlineDate!.timeIntervalSince1970{
                    timer.invalidate()
                }
                
                let leastTime = Int(weakSelf!.offlineDate!.timeIntervalSince1970 - newDate.timeIntervalSince1970)
                let day:Int  = leastTime / (60 * 60 * 24)
                let hour:Int  = (leastTime % (60 * 60 * 24)) / (60 * 60)
                let minute:Int  = (leastTime % (60 * 60)) / 60
                let second:Int  = leastTime % 60
                weakSelf?.dayLabel?.text = String.init(format: " %02d ", arguments: [day])
                weakSelf?.hourLabel?.text = String.init(format: " %02d ", arguments: [hour])
                weakSelf?.minuteLabel?.text = String.init(format: " %02d ", arguments: [minute])
                weakSelf?.secondLabel?.text = String.init(format: " %02d ", arguments: [second])
            })
            RunLoop.main.add(self.timer!, forMode: RunLoop.Mode.common)
        }
    }
    
    deinit {
        self.timer?.invalidate()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        
        let yellowBackImage:UIImageView = UIImageView.init()
        yellowBackImage.backgroundColor = YellowColor
        self.addSubview(yellowBackImage)
        yellowBackImage.snp.makeConstraints { (make) in
            make.left.right.top.equalTo(0)
            make.height.equalTo(SizeFitFloat(202))
        }
        
        let yellowCornerImage:UIImageView = LGTViewCreateUtil.share.createImageViewCorner(corner: SizeFitFloat(20))
        self.addSubview(yellowCornerImage)
        yellowCornerImage.backgroundColor = YellowColor
        yellowCornerImage.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.height.equalTo(SizeFitFloat(50))
            make.top.equalTo(yellowBackImage.snp_bottomMargin).offset(-SizeFitFloat(25))
        }
        
        self.contentView.backgroundColor = ViewBackColor
        let contentBackView:UIImageView = LGTViewCreateUtil.share.createImageViewCorner(corner: SizeFitFloat(5))
        contentBackView.backgroundColor = UIColor.white
        contentBackView.isUserInteractionEnabled = true
        self.addSubview(contentBackView)
        contentBackView.snp.makeConstraints { (make) in
            make.top.equalTo(SizeFitFloat(10))
            make.left.equalTo(SizeFitFloat(6))
            make.right.equalTo(SizeFitFloat(-8))
            make.height.equalTo(SizeFitFloat(274))
            make.bottom.equalTo(0)
        }
    
//        self.cornerBackView = LGTCornerView()
//        self.cornerBackView?.backgroundColor = UIColor.white
//        self.cornerBackView?.cornerSize = CGSize.init(width: SizeFitFloat(25), height: SizeFitFloat(25))
//        self.cornerBackView?.backColor = UIColor.orange
//        self.cornerBackView?.type = .BottomRightCornerTypeType
        self.cornerBackView = LGTViewCreateUtil.share.createImageView(image: "")
        contentBackView.addSubview(self.cornerBackView!)
        
        self.stateLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: UIColor.white, fontSize: SizeFitFloat(15), text: "哈哈哈哈哈")
        contentBackView.addSubview(self.stateLabel!)
        self.stateLabel!.snp.makeConstraints { (make) in
            make.center.equalTo(self.cornerBackView!)
            make.height.equalTo(SizeFitFloat(15))
        }
        
        self.cornerBackView!.snp.makeConstraints { (make) in
            make.top.left.equalTo(0)
            make.right.equalTo(self.stateLabel!.snp_rightMargin).offset(SizeFitFloat(20))
            make.height.equalTo(SizeFitFloat(28))
           // make.width.equalTo(100)
        }
        
        let taskInfoLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(15), text: "任务信息")
        contentBackView.addSubview(taskInfoLabel)
        taskInfoLabel.snp.makeConstraints { (make) in
            make.height.equalTo(SizeFitFloat(15))
            make.top.equalTo(SizeFitFloat(51))
            make.left.equalTo(SizeFitFloat(17))
        }
        
        let lookBtn:UIButton = LGTViewCreateUtil.share.createTextLGTButton(title: "查看操作记录", font: Font_Normal_15, color: YellowColor, selectedColor: YellowColor, corner: SizeFitFloat(16), backColor: UIColor.white, borderWidth: 1, borderColor: YellowColor)
        contentBackView.addSubview(lookBtn)
        lookBtn.snp.makeConstraints { (make) in
            make.width.equalTo(SizeFitFloat(114))
            make.height.equalTo(SizeFitFloat(32))
            make.top.equalTo(SizeFitFloat(24))
            make.right.equalTo(-SizeFitFloat(18))
        }
        lookBtn.addTarget(self, action: #selector(gotoAction), for: UIControl.Event.touchUpInside)
        
        self.completeCountLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.right, textColor: TextColor, fontSize: SizeFitFloat(15), text: "1000")
        self.completeCountLabel?.font = KBFont(size: 20)
        contentBackView.addSubview(self.completeCountLabel!)
        self.completeCountLabel!.snp.makeConstraints { (make) in
            make.right.equalTo(contentBackView.snp_centerXWithinMargins).offset(SizeFitFloat(-29))
            make.height.equalTo(SizeFitFloat(15))
            make.top.equalTo(SizeFitFloat(80))
        }
        
        let completeLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: SizeFitFloat(15), text: "已完成")
        contentBackView.addSubview(completeLabel)
        completeLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.completeCountLabel!)
            make.height.equalTo(SizeFitFloat(15))
            make.top.equalTo(SizeFitFloat(106))
        }
        
        self.unCompleteCountLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(15), text: "1000")
        self.unCompleteCountLabel?.font = KBFont(size: 20)
        contentBackView.addSubview(self.unCompleteCountLabel!)
        self.unCompleteCountLabel!.snp.makeConstraints { (make) in
            make.left.equalTo(contentBackView.snp_centerXWithinMargins).offset(SizeFitFloat(29))
            make.height.equalTo(SizeFitFloat(15))
            make.top.equalTo(SizeFitFloat(80))
        }
        
        let uncompleteLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: SizeFitFloat(15), text: "未完成")
        contentBackView.addSubview(uncompleteLabel)
        uncompleteLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.unCompleteCountLabel!)
            make.height.equalTo(SizeFitFloat(15))
            make.top.equalTo(SizeFitFloat(106))
        }
        
        
        let unTaskInfoLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(15), text: "未完成概况")
        contentBackView.addSubview(unTaskInfoLabel)
        unTaskInfoLabel.snp.makeConstraints { (make) in
            make.height.equalTo(SizeFitFloat(15))
            make.top.equalTo(SizeFitFloat(142))
            make.left.equalTo(SizeFitFloat(17))
        }
        
        //时间标签
        let lastTimeLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(12), text: "距离过期还剩")
        contentBackView.addSubview(lastTimeLabel)
        lastTimeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFitFloat(49))
            make.height.equalTo(SizeFitFloat(12))
            make.bottom.equalTo(-SizeFit(20))
        }
        
        self.dayLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(12), text: " 00 ")
        self.dayLabel?.backgroundColor = ViewBackColor
        contentBackView.addSubview(self.dayLabel!)
        self.dayLabel!.snp.makeConstraints { (make) in
            make.left.equalTo(lastTimeLabel.snp_rightMargin).offset(SizeFit(12))
            make.height.equalTo(SizeFit(20))
            make.centerY.equalTo(lastTimeLabel)
        }
        
        let dayLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(12), text: "天")
        contentBackView.addSubview(dayLabel)
        dayLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.dayLabel!.snp_rightMargin).offset(SizeFit(12))
            make.height.equalTo(SizeFit(20))
            make.centerY.equalTo(lastTimeLabel)
        }
        
        self.hourLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(12), text: " 00 ")
        self.hourLabel?.backgroundColor = ViewBackColor
        contentBackView.addSubview(self.hourLabel!)
        self.hourLabel!.snp.makeConstraints { (make) in
            make.left.equalTo(dayLabel.snp_rightMargin).offset(SizeFit(12))
            make.height.equalTo(SizeFit(20))
            make.centerY.equalTo(lastTimeLabel)
        }
        
        let hourLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(12), text: "时")
        contentBackView.addSubview(hourLabel)
        hourLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.hourLabel!.snp_rightMargin).offset(SizeFit(12))
            make.height.equalTo(SizeFit(20))
            make.centerY.equalTo(lastTimeLabel)
        }
        
        self.minuteLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(12), text: " 00 ")
        self.minuteLabel?.backgroundColor = ViewBackColor
        contentBackView.addSubview(self.minuteLabel!)
        self.minuteLabel!.snp.makeConstraints { (make) in
            make.left.equalTo(hourLabel.snp_rightMargin).offset(SizeFit(12))
            make.height.equalTo(SizeFit(20))
            make.centerY.equalTo(lastTimeLabel)
        }

        let minuteLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(12), text: "分")
        contentBackView.addSubview(minuteLabel)
        minuteLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.minuteLabel!.snp_rightMargin).offset(SizeFit(12))
            make.height.equalTo(SizeFit(20))
            make.centerY.equalTo(lastTimeLabel)
        }

        self.secondLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(12), text: " 00 ")
        self.secondLabel?.backgroundColor = ViewBackColor
        contentBackView.addSubview(self.secondLabel!)
        self.secondLabel!.snp.makeConstraints { (make) in
            make.left.equalTo(minuteLabel.snp_rightMargin).offset(SizeFit(12))
            make.height.equalTo(SizeFit(20))
            make.centerY.equalTo(lastTimeLabel)
        }

        let secondLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(12), text: "秒")
        contentBackView.addSubview(secondLabel)
        secondLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.secondLabel!.snp_rightMargin).offset(SizeFit(12))
            make.height.equalTo(SizeFit(20))
            make.centerY.equalTo(lastTimeLabel)
        }
        
        self.statueScrollView = UIScrollView.init()
        //self.statueScrollView?.backgroundColor = UIColor.orange
        contentBackView.addSubview(self.statueScrollView!)
        self.statueScrollView!.snp.makeConstraints { (make) in
            make.left.equalTo(0)
            make.top.equalTo(SizeFit(185))
            make.height.equalTo(SizeFit(34))
            make.width.equalTo(KScreenW)
        }
        
    }
    
    @objc func gotoAction(){
        if self.clickBlock != nil{
            self.clickBlock!(0)
        }
    }
}
